/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.repository.entity;

import com.inspur.edp.caf.generator.baseInfo.AnnotationInfo;
import com.inspur.edp.caf.generator.baseInfo.AnnotationType;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.caf.generator.property.ClassPropertyGenerator;
import com.inspur.edp.cdf.component.api.schema.CommonComponent;
import com.inspur.edp.cef.generator.repository.base.CefRepoGenCtx;
//import com.inspur.edp.cef.generator.repository.entity.methods.GetAdapterExtendClassesMethodGenerator;
import com.inspur.edp.cef.generator.repository.entity.properties.*;
import com.inspur.edp.cef.repository.assembler.entity.MainEntityAssembler;
import com.inspur.edp.cef.repository.repo.RepositoryExtendAttribute;
import com.inspur.edp.cef.spi.jsonser.abstractcefchange.BefDateTimeDeseiralizer;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import lombok.var;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.TypeLiteral;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;

public abstract class MainEntityAssemblerGenerator extends EntityAssmeblerGenerator {
  private IGspCommonObject commonObject;

  public MainEntityAssemblerGenerator(CefRepoGenCtx repoGenCtx, IGspCommonObject dataType) {
    super(repoGenCtx, dataType);
    this.commonObject = dataType;
  }

  @Override
  protected TypeInfo setParentClass() {
    return new TypeInfo(MainEntityAssembler.class);
  }


  @Override
  protected ArrayList<ClassPropertyGenerator> createPropertyGenerators() {
    ArrayList<ClassPropertyGenerator> list = super.createPropertyGenerators();
    if (list == null) {
      list = new ArrayList<ClassPropertyGenerator>();
    }
    list.add(getIsFiscalPropGenerator());
    list.add(getTableAliasPropGenerator());
    list.add(getTableNamePropGenerator());
    list.add(getNodeCodePropGenerator());
    list.add(getParentAssmeblerGenerator());
    list.add(getDboIDGenerator());
    return list;
  }

  protected abstract IsFiscalTablePropertyGenerator getIsFiscalPropGenerator();

  protected abstract DboIDPropertyGenerator getDboIDGenerator();

  protected MainEntityTableAliasPropertyGenerator getTableAliasPropGenerator() {
    return new MainEntityTableAliasPropertyGenerator(commonObject.getCode());
  }

  protected abstract MainEntityTableNamePropertyGenerator getTableNamePropGenerator();

  protected NodeCodePropertyGenerator getNodeCodePropGenerator() {
    return new NodeCodePropertyGenerator(commonObject.getCode());
  }

  protected ParentAssemblerPropertyGenerator getParentAssmeblerGenerator() {
    return new ParentAssemblerPropertyGenerator(
        commonObject.getParentObject() == null ? "" : commonObject.getParentObject().getCode());
  }

  @Override
  protected ArrayList<AnnotationInfo> getAttributeList() {

    ArrayList<AnnotationInfo> list = super.getAttributeList();
    if (list == null) {
      list = new java.util.ArrayList<AnnotationInfo>();
    }
    var reposComps = commonObject.getRepositoryComps();
    if(repoGenCtx.getCefGenCtx().getMetadata().getHeader().isExtendable()){
      list.add(buildReposExtendInfoAnnotation("com.inspur.edp.customize.repository.CustomizeExtendInfo"));
    }
    if(reposComps == null || reposComps.size()<1)
      return list;
//    for (var compInfo: reposComps) {
//      var compMetadata = repoGenCtx.getCefGenCtx().getMetadataJitContext().getMetadata(compInfo.getId());
//      var comp = (CommonComponent)compMetadata.getContent();
//      comp.get
//    }

    return list;
  }

  private AnnotationInfo buildReposExtendInfoAnnotation(String className){
    AnnotationInfo info = new AnnotationInfo();
    TypeInfo typeInfo = new TypeInfo(RepositoryExtendAttribute.class);
    info.setTypeInfo(typeInfo);
    info.setAnnotationType(AnnotationType.SingleMember);

    TypeLiteral annotationParam = ast.newTypeLiteral();
    annotationParam.setType(ast.newSimpleType(ast.newName(className)));

    info.setSingleParam(annotationParam);
    return info;
  }

  @Override
  protected ArrayList<ClassMethodGenerator> createMethodGenerators() {
    ArrayList<ClassMethodGenerator> list = super.createMethodGenerators();
    if(list ==null)
      list=new ArrayList<>();
//    list.add(new GetAdapterExtendClassesMethodGenerator(commonObject));
    return list;
  }
}
